package com.wcs.exam.controller.auth;

import com.wcs.exam.common.base.Result;
import com.wcs.exam.common.base.page.Page;
import com.wcs.exam.controller.auth.biz.AuthExamBiz;
import com.wcs.exam.controller.auth.req.*;
import com.wcs.exam.controller.auth.resp.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * Auth-考试
 *
 * @author wcs
 * @since 2024-07-10
 */
@Tag(name = "Auth-考试相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/auth/exam")
public class AuthExamController {

    @NotNull
    private final AuthExamBiz biz;

    @Operation(summary = "考试搜索")
    @GetMapping(value = "/search")
    public Result<Page<AuthExamPageResp>> search(@ModelAttribute @Valid AuthExamPageReq req) {
        return biz.search(req);
    }

    @Operation(summary = "考试详情，可以判断出登录用户是否可以考试")
    @Parameter(name = "id", description = "考试ID", required = true)
    @GetMapping(value = "/view")
    public Result<AuthExamViewResp> view(@RequestParam Long id) {
        return biz.view(id);
    }

    @Operation(summary = "考试报名", description = "正式考试类型才需要进行报名")
    @PostMapping(value = "/registration")
    public Result<String> registration(@RequestBody @Valid AuthExamRegistrationReq req) {
        return biz.registration(req);
    }

    @Operation(summary = "考试状态检查", description = "校验用户在某考试下是否处于考试状态")
    @GetMapping(value = "/check")
    public Result<AuthExamCheckResp> check(@ModelAttribute @Valid AuthExamCheckReq req) {
        return biz.check(req);
    }

    @Operation(summary = "开始考试")
    @PostMapping(value = "/start")
    public Result<AuthExamStartResp> examStart(@RequestBody @Valid AuthExamStartReq req) {
        return biz.examStart(req);
    }

    @Operation(summary = "继续考试")
    @PostMapping(value = "/continue")
    public Result<AuthExamContinueResp> examContinue(@RequestBody @Valid AuthExamContinueReq req) {
        return biz.examContinue(req);
    }

    @Operation(summary = "考试结束", description = "考试结果需要单独获取")
    @PostMapping(value = "/end")
    public Result<String> examEnd(@RequestBody @Valid AuthExamEndReq req) {
        return biz.examEnd(req);
    }

    @Operation(summary = "答题", description = "考试保存答案")
    @PostMapping(value = "/save/answer")
    public Result<String> saveAnswer(@RequestBody @Valid AuthExamSaveAnswerReq req) {
        return biz.saveAnswer(req);
    }

    @Operation(summary = "记录切屏次数", description = "返回剩余次数")
    @PutMapping(value = "/update/switch/screen/count")
    public Result<Integer> updateSwitchScreenCount(@RequestBody @Valid AuthExamUpdateSwitchScreenCountReq req) {
        return biz.updateSwitchScreenCount(req);
    }

    @Operation(summary = "考试报告")
    @Parameter(name = "recordId", description = "考试记录ID", required = true)
    @GetMapping(value = "/report")
    public Result<AuthExamReportResp> report(@RequestParam @NotNull(message = "resource.exam.record.id.not.null") Long recordId) {
        return biz.report(recordId);
    }

    @Operation(summary = "作答解析", description = "查看考试后的作答信息、答案和题目分析等信息")
    @Parameter(name = "recordId", description = "考试记录ID", required = true)
    @GetMapping(value = "/parse")
    public Result<AuthExamParseResp> parse(@RequestParam @NotNull(message = "resource.exam.record.id.not.null") Long recordId) {
        return biz.parse(recordId);
    }

    @Operation(summary = "人脸错误申诉", description = "只有正式考试，开考前人脸识别错误且开启了允许申诉才能进行申诉")
    @PostMapping(value = "/face/error/appeal")
    public Result<String> faceErrorAppeal(@RequestBody @Valid AuthExamFaceErrorAppealReq req) {
        return biz.faceErrorAppeal(req);
    }

    @Operation(summary = "考试记录分页列出")
    @GetMapping(value = "/record/page")
    public Result<Page<AuthExamRecordPageResp>> recordPage(@ModelAttribute @Valid AuthExamRecordPageReq req) {
        return biz.recordPage(req);
    }

    @Operation(summary = "考试-考试排行榜-分页", description = "考试-考试排行榜-分页")
    @GetMapping(value = "/rank/page")
    public Result<Page<AuthExamRankPageResp>> rankPage(@ModelAttribute @Valid AuthExamRankPageReq req) {
        return biz.rankPage(req);
    }

    @Operation(summary = "考试-自身排名", description = "考试-自身排名")
    @GetMapping(value = "/rank/self")
    public Result<AuthExamRankSelfResp> rankSelf(@ModelAttribute @Valid AuthExamRankSelfReq req) {
        return biz.selfRank(req);
    }

    /*---------------------------------------------------- 个人中心 ----------------------------------------------------*/

    @Operation(summary = "用户中心-考试分页列出")
    @GetMapping(value = "/user/page")
    public Result<Page<AuthExamUserPageResp>> userPage(@ModelAttribute @Valid AuthExamUserPageReq req) {
        return biz.userPage(req);
    }
}
